/************************************************************
 ********************** 左表格*******************************
 ***********************************************************/
/**
 /**
 * 数据中心字典列表初始化
 */
var SdsStandCdcDic = {
    id: "cdcDicTree",	//表格id
    seItem: null,		                //选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    dicId: 0
};


/**
 * 根据标准化字典子项id查找关联医嘱项
 */
SdsStandCdcDic.onClickDicSub = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体字典。");
        return false;
    }
    if (treeNode.isParent) {
        //字典项
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体字典。");
        return false;
    }
    SdsStandCdcDic.dicId = treeNode.id;
    //查询单病种字典对照
    SdsStandCdcMatch.seItem = null;
    SdsStandCdcMatch.search();
}

/************************************************************
 ********************** 右表格部分*******************************
 ***********************************************************/
/**
 /**
 * 标准化_医嘱项目配置管理初始化
 */
var SdsStandCdcMatch = {
    id: "SdsStandCdcMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsStandCdcMatch.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true,formatter : selectcheckbox},
        {title: '字典代码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '字典名称', field: 'description', align: 'center', valign: 'middle', sortable: true},
        {title: '字典分类', field: 'dictypeDesc', align: 'center', valign: 'middle', sortable: true},
    ];
    function selectcheckbox(value, row, index) {
        if(row.isCheck){
            return {
                disabled : false,
                checked : true
            };
            return value;
        }
    }
};

/**
 * 检查是否选中
 */
SdsStandCdcMatch.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsStandCdcMatch.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增标准化_医嘱项配置
 */
SdsStandCdcMatch.add = function () {
    var index = Sys.open('新增标准化_医嘱对照',Sys.backendPath + '/sds/sdsstandordermatch/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看标准化_医嘱项配置详情
 */
SdsStandCdcMatch.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('标准化_医嘱对照详情',Sys.backendPath + '/sds/sdsstandordermatch/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除标准化_医嘱项配置
 */
SdsStandCdcMatch.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/delete", function (data) {
                Sys.success("删除成功!");
                SdsStandCdcMatch.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsStandOrderMatchId",id);
            ajax.start();
        };
        Sys.confirm("是否删除标准化_医嘱对照?",operation);
    }
};

/**
 * 同步医嘱项表到标准化_医嘱项配置表
 */
SdsStandCdcMatch.sync = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/sync", function (data) {
            Sys.success("同步成功!");
            SdsStandCdcMatch.table.refresh();
        }, function (data) {
            Sys.error("同步失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否同步医嘱项到标准化_医嘱对照?",operation);
}

/**
 * 对照
 */
SdsStandCdcMatch.compare = function (){
    var selected = $('#SdsStandCdcMatchTable').bootstrapTable('getSelections');
    if (SdsStandCdcMatch.dicId&&SdsStandCdcMatch.dicId!=0){
        //标准字典对照
        var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandcdcmatch/compare", function (data) {
            Sys.success("对照成功!");
            SdsStandCdcMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("cdcDicId",SdsStandCdcDic.dicId);
        ajax.set("sdsDicIds",idStr);
        ajax.start();
    } else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

$("#SdsStandCdcMatchTable").on("check.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandcdcmatch/compare", function (data) {
        Sys.success("对照成功!");
        SdsStandCdcMatch.table.refresh();
    }, function (data) {
        Sys.error("对照失败!" + data.responseJSON.message + "!");
    });
    ajax.set("cdcSysDicId",row.id);
    if (SdsStandCdcDic.dicId&&SdsStandCdcDic.dicId!=0){
        ajax.set("cdcCtDicId",SdsStandCdcDic.dicId);
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }
    ajax.start();

});

/**
 * 撤销
 */
$("#SdsStandCdcMatchTable").on("uncheck.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandcdcmatch/undo", function (data) {
        Sys.success("撤销成功!");
        SdsStandCdcMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("cdcSysDicId",row.id);
    if (SdsStandCdcDic.dicId&&SdsStandCdcDic.dicId!=0){
        ajax.set("cdcCtDicId",SdsStandCdcDic.dicId);
    }

    ajax.start();
});

SdsStandCdcMatch.undo = function (){
    var selected = $('#SdsStandCdcMatchTable').bootstrapTable('getSelections');
    if(selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        SdsStandCdcMatch.seItem = selected[0];
        var id = SdsStandCdcMatch.seItem.id;
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstandlabitemmatch/undo", function (data) {
                Sys.success("撤销成功!");
                SdsStandCdcMatch.table.refresh();
            }, function (data) {
                Sys.error("撤销失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsStandLabitemMatchId",id);
            ajax.start();
        };
        Sys.confirm("是否撤销对照关系?",operation);
    }
}

/**
 * 未对照
 */
SdsStandCdcMatch.compareNo = function (){
    if (SdsStandCdcDic.dicId&&SdsStandCdcDic.dicId!=0){
        var queryData = {};
        queryData['ctDic'] = SdsStandCdcDic.dicId;
        queryData['compare'] = "noMatch";
        SdsStandCdcMatch.table.refresh({query: queryData});
    } else {
        Sys.info("请先选中某个字典！");
        return false;
    }

}


/**
 * 已对照
 */
SdsStandCdcMatch.compareYes = function (){
    if (SdsStandCdcDic.dicId&&SdsStandCdcDic.dicId!=0){
        var queryData = {};
        queryData['ctDic'] = SdsStandCdcDic.dicId;
        queryData['compare'] = "match";
        SdsStandCdcMatch.table.refresh({query: queryData});
    } else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 全部
 */
SdsStandCdcMatch.compareAll = function (){
    if (SdsStandCdcDic.dicId&&SdsStandCdcDic.dicId!=0){
        var queryData = {};
        queryData['ctDic'] = SdsStandCdcDic.dicId;
        queryData['compare'] = "all";
        SdsStandCdcMatch.table.refresh({query: queryData});
    } else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 自动匹配
 */
SdsStandCdcMatch.autoCompare = function (){
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsstandordermatch/autoCompare", function (data) {
            Sys.success("自动匹配成功!");
            SdsStandCdcMatch.table.refresh();
        }, function (data) {
            Sys.error("自动匹配失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };
    Sys.confirm("是否进行自动匹配?",operation);
}

/**
 * 查询标准化_医嘱项配置表
 */
SdsStandCdcMatch.search = function () {
    var queryData = {}
    queryData['ctDic'] = SdsStandCdcDic.dicId;
    queryData['description'] = $("#seachCdcKey").val();
    SdsStandCdcMatch.table.refresh({query: queryData});
};

$('#seachCdcKey').keydown(function(event){
    if(event.keyCode==13){
        //grid.submitFilter();
        SdsStandCdcMatch.search();
    }
});

$(function () {
    //查询数据中心字典树
    var ztree = new $ZTree("cdcDicTree", Sys.backendPath + "/cdc/cdcdata/treeAll/null/null");
    ztree.bindOnClick(SdsStandCdcDic.onClickDicSub);
    ztree.init();
    SdsStandCdcDic.ztree = ztree;
    fuzzySearch('cdcDicTree','#seachKey',null,true);

    //对照的字典颜色改变
    var ajax = new $ax(Sys.backendPath + "/sds/sdsstandcdcmatch/matchDicsub", function (data) {
        var treeObj = $.fn.zTree.getZTreeObj("cdcDicTree");
        for (var i=0;i<data.length;i++){
            var node = treeObj.getNodeByParam("id", data[i]);
            if (node != null){
                $("#"+node.tId+"_span").css("color",'red');
            }
        }
    }, function (data) {
        Sys.error("渲染失败!" + data.responseJSON.message + "!");
    });
    ajax.start();


    var defaultColunms = SdsStandCdcMatch.initColumn();
    var table = new BSTable(SdsStandCdcMatch.id, Sys.backendPath + "/sds/sdsstandcdcmatch/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setSubHeight(-10);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    //table.set("compare", "");
    table.set("compare", "match");
    SdsStandCdcMatch.table = table.init();
});
